﻿using Models;
using SqlSugar;

namespace UserManage
{
    public class Class1
    {
       
        public void Fun()
        {
            string appPath = AppContext.BaseDirectory;

            ConnectionConfig connectionConfig = new ConnectionConfig();
            connectionConfig.DbType = DbType.Sqlite;
            connectionConfig.ConnectionString = @"Data Source=cms.db;";
            using (SqlSugarClient sugarClient = new SqlSugarClient(connectionConfig))
            {
                sugarClient.DbFirst.CreateClassFile(@"{appPath}\UserManage\dbModels");
                //插入的操作
                admin _admin = new admin()
                {
                    id = 4,
                    name = "测试",
                };
                sugarClient.Insertable<admin>(_admin).ExecuteCommand();
                admin admin0 = sugarClient.Queryable<admin>().First();
                print(admin0);
                //查询的操作
                admin admin1 = sugarClient.Queryable<admin>().First();
                print(admin1);
                //更新的操作
                _admin.name = _admin.name + "更新后";
                sugarClient.Updateable<admin>(_admin).WhereColumns(it => new { it.id }).ExecuteCommand();
                admin admin2 = sugarClient.Queryable<admin>().First();
                print(admin2);
                //删除的操作
                sugarClient.Deleteable<admin>().In(it => it.id, 4).ExecuteCommand();
                admin admin3 = sugarClient.Queryable<admin>().First();
                print(admin3);
                Console.ReadLine();

            }
            
        }
        public static void print(admin _admin)
        {
            if (_admin == null)
            {
                Console.WriteLine("数据库中没有数据");
            }
            else
            {
                Console.WriteLine($"id = {_admin.id} ---- content = {_admin.name} ");
            }

        }
    }
}
